package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.service.SetMealService;
import com.sky.utils.RedisUtil;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private SetMealDishMapper setMealDishMapper;

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 新增套餐
     *
     * @param setmeal
     * @param setmealDishes
     * @return
     */
    @Override
    @Transactional
    @AutoFill(OperationType.INSERT)
    public void addSetMeal(Setmeal setmeal, List<SetmealDish> setmealDishes) {
        setMealMapper.insert(setmeal);
        setmealDishes.forEach(sd -> sd.setSetmealId(setmeal.getId()));
        setMealDishMapper.insert(setmealDishes);
    }

    /**
     * 分页查询套餐
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageSetMeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 构造分页对象
        Page<SetmealVO> setmealPage = new Page<>(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        // 构造分页查询条件
        setmealPage = setMealMapper.setMealPage(setmealPage, setmealPageQueryDTO);
        return new PageResult(setmealPage.getTotal(), setmealPage.getRecords());
    }


    /**
     * 批量删除套餐
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public void deleteSetMeal(List<Long> ids) {
        // 查询套餐销售状态
        List<Setmeal> setmeals = setMealMapper.selectByIds(ids);
        setmeals.forEach(setmeal -> {
            if (Objects.equals(setmeal.getStatus(), StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        // 查询套餐关联的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<SetmealDish>()
                .in(SetmealDish::getSetmealId, ids);
        setMealDishMapper.delete(queryWrapper);
        // 删除套餐
        setMealMapper.deleteByIds(ids);
    }

    /**
     * 修改套餐
     *
     * @param setmeal
     * @param setmealDishes
     * @return
     */
    @Override
    @Transactional
    @AutoFill(OperationType.UPDATE)
    public void updateSetMeal(Setmeal setmeal, List<SetmealDish> setmealDishes) {
        // 修改套餐
        setMealMapper.updateById(setmeal);
        // 删除套餐关联的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<SetmealDish>()
                .eq(SetmealDish::getSetmealId, setmeal.getId());
        setMealDishMapper.delete(queryWrapper);
        // 插入新的菜品
        setmealDishes.forEach(sd -> sd.setSetmealId(setmeal.getId()));
        setMealDishMapper.insert(setmealDishes);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetMealById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        // 查询套餐
        Setmeal setmeal = setMealMapper.selectById(id);
        BeanUtils.copyProperties(setmeal, setmealVO);
        // 查询套餐关联的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<SetmealDish>()
                .eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setMealDishMapper.selectList(queryWrapper);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐起售状态
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    public void updateSetMealStatus(Integer status, Long id) {
        if (Objects.equals(status, StatusConstant.ENABLE)) {
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<SetmealDish>()
                    .eq(SetmealDish::getSetmealId, id);
            List<SetmealDish> setmealDishes = setMealDishMapper.selectList(queryWrapper);
            setmealDishes.forEach(sd -> {
                DishVO dishVO = dishService.getDishWithFlavorById(sd.getDishId());
                if (dishVO.getStatus().equals(StatusConstant.DISABLE)){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setMealMapper.updateById(setmeal);
    }

    /**
     * 根据分类id查询套餐
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> getSetMealByCategoryId(Long categoryId) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<Setmeal>()
                .eq(Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, StatusConstant.ENABLE);
        return setMealMapper.selectList(queryWrapper);
    }

    /**
     * 根据套餐id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public List<DishVO> getDishBySetMealId(Long id) {
        //查询套餐关联的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<SetmealDish>()
                .eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setMealDishMapper.selectList(queryWrapper);
        //获取菜品
        List<DishVO> dishVOs = setmealDishes.stream().map(setmealDish -> {
            Long dishId = setmealDish.getDishId();
            return dishService.getDishWithFlavorById(dishId);
        }).collect(Collectors.toList());
        return dishVOs;
    }
}
